<?php
##############
#	HOCTIL NDS     #
###################################################################################
 
 /**
    * FILE:          		cieEdit.php
    * LOCATION:      /root
    * FUNCTION:      this is the main interface router/controller.  all user request to handle consumer records is 
    * managed by this file.  it renders the consumer meta-data tab area.  it also displays the FORMS CONTROLLER
   * if an existing consumer record is in the current view.
    * DEPENDENCIES:  db_connect.php, scriptaculous js code
    * ==============================================================================
    * @version $Id: cieEdit.php,v 0.2 2009/01/01
    * @copyright Copyright (c) none
    * @author ChrisV
    * ==============================================================================
    */
   
   #############
   #	dependencies    #
   #############

   #  connect to database
   include('includes/db_connect.php');



   #	get requested consumer and form needed to update record  
   $id   = (int)sanitize($_GET['id']);
   $form = (string)sanitize($_GET['form']);
  
  
  #	render correct record control options 
  # for user
   if(strcmp($id, '0')==0) {
       #  consumer id is 0, new consumer file being made
	$noRecord = true;
	$btnName  = 'Create Record';
	$title    = 'Create New Record';
   }else{
	 # existing record is requested and found
	$noRecord = false;
	$btnName  = 'Update Record';
	$query    = "SELECT c.* 	
				  FROM consumer c 
				  WHERE c.id=".mysql_real_escape_string($id);              
       //echo $query."<br/>";
       $result   = mysql_query($query);
	   
	 # define consumer meta-data for top of "file tab"  
	if($row=mysql_fetch_array($result)) {
   	       $lName     =mysql_real_escape_string($row["last_name"]);
		$fName     =mysql_real_escape_string($row["first_name"]);
		$phone     =mysql_real_escape_string($row["phone"]);
		$address   =mysql_real_escape_string($row["address"]);
		$city      =mysql_real_escape_string($row["city"]);
		$state     =mysql_real_escape_string($row["state_id"]);
		$zip       =mysql_real_escape_string($row["zip"]);
		
		# woodie's additions -- VAIL
		$mInitial       	=mysql_real_escape_string($row["middle_initial"]);
		$social_security_number  =mysql_real_escape_string($row["social_number"]);
		$medicaid_number       =mysql_real_escape_string($row["medicaid_number"]);
		$medicare_number       =mysql_real_escape_string($row["medicare_number"]);
		$county       =mysql_real_escape_string($row["county"]);
	   } 
	   
      # show consumer info to user for edited record	
      $title = 'Edit Record: <div class="gt-success">'.$fName.' '.$lName.'</div>';
   }

	# simple status message for user:  shows Success or Fail for data processing
   if(sanitize($_GET["Status"])) {
?>
	<p style="font-weight:bold; font-size: 1.3em;"><?php echo $_GET["Status"]; ?></p>
<?php 
   }
?>

   <h2><?php echo $title; ?></h2>
   
   <!-- BEGIN CONSUMER METADATA FORM-->
   <form action="actions/ProcessConsumerUpdate.php" method="post">
		<div style="background:#eee; border:1px #ddd solid;padding:3px;">
			<table class="consumer_tab">
				<tr>
  <?php                     
					#we pulled up and existing file, make consumer id impossible to edit
					if(!$noRecord) {                 
?>
						<td style="width:60px;">
			                        <div class="formUnitSml" style="width: 160px;">
										<label class="readable">Consumer Id:</label>
										<input class="data" 
														style="width: 50px;"
														type="text" 
														name="consumerId" readonly disabled value="<?php echo $id ?>"/>
			                        </div>
						</td>
 <?php                   
					} 
					#okay, the rest is easy.  it will render the remaining parts of the user interface
					#things don't get interesting until we get to the controller area
?>					
					<td style="width:300px;">
						<div class="formUnitSml">
							<label class="readable">First Name:</label>
							<input class="data" style="width:130px;" type="text" name="fName" value="<?php echo $fName ?>"/>
						</div>
					</td>
					<td>
						<div class="formUnitSml">
							<label class="readable">Last Name:</label>
							<input class="data" style="width:130px;" type="text" name="lName" value="<?php echo $lName ?>"/>
						</div>
					</td>
					<td id="middleinitial">
						<div class="formUnitSml">
							<label class="readable">Middle Initial:</label>
							<input class="data" style="width:25px;"type="text" name="mInitial" value="<?php echo $mInitial ?>"/>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width:350px;">    
						<div class="formUnitSml">
							<label class="readable"><span class="privateinfo">Social Security #:</span></label>
							<input class="data" type="text" name="phone" value="<?php echo $social_security_number ?>" style="width:100px"/>
						</div>
					</td>
					<td style="width:300px;">    
						<div class="formUnitSml">
							<label class="readable"><span class="privateinfo">Medicaid #:</span></label>
							<input class="data" type="text" name="phone" value="<?php echo $medicaid_number ?>" style="width:100px"/>
						</div>
					</td>
					<td style="width:300px;">    
						<div class="formUnitSml">
							<label class="readable"><span class="privateinfo">Medicare #:</span></label>
							<input class="data" type="text" name="phone" value="<?php echo $medicare_number ?>" style="width:100px"/>
						</div>
					</td>
					<td>    
						<div class="formUnitSml">
							<label class="readable">Phone:</label>
							<input class="data" type="text" name="phone" value="<?php echo $phone ?>" style="width:100px"/>
						</div>
					</td>
				</tr>  
				<tr>
					<td colspan="9">
						<p class="gt-error" style="text-align:center;margin-top:1px; margin-bottom:1px;">
							<strong>All consumer information with red background labels is
							* PRIVATE DATA *.  DO NOT disclose this data WITHOUT WRITTEN CONSENT.</strong>
						</p>
					</td>
				</tr>
			</table>
			<table width="100%" style="margin-top:5px;" class="consumer_tab">
				<tr>
					<td colspan="3" style="padding-top:5px;">
						<div class="formUnitSml" style="width:400px">
							<label class="readable">Address:</label>
							<input class="data" style="width: 300px;"type="text" name="address" value="<?php echo $address ?>" style="width:350px"/>
						</div>
					</td>
					<td>
						<div class="formUnitSml" style="width:240px">
							<label style="width:50px" class="readable">City:</label>
							<input class="data" type="text" name="city" style="width:150px" value="<?php echo $city ?>"/>
						</div>
					</td>
					<td>
						<div class="formUnitSml" style="width:130px">
							<label style="width:40px" class="readable">State:</label>
								<select name="state" style="width:70px" class="data" >
 <?php 
								#get our list of states and render the selected state from the db
								$stateQuery = "SELECT * FROM lst_state";
								$result     = mysql_query($stateQuery);         
								while($row=mysql_fetch_array($result)) {    
									$state_id = $row["id"];
									$abbr     = $row["abbr"];          
						
									if($state==$state_id){           
										#stored db value, so select it
										echo "<option value=\"".$state_id."\" selected>".$abbr."</option>";       
									}else{           
										echo "<option value=\"".$state_id."\">".$abbr."</option>";             
									}       
								}         
?>
								</select>
						</div>
					</td>
				</tr>
				<tr>
					<td>          
						<div class="formUnitSml" style="width:150px">
							<label style="width:70px" class="readable">County:</label>
							<input class="data" type="text" name="county" style="width:60px" value="<?php echo $county ?>"/>
						</div>
					</td>
					<td>          
						<div class="formUnitSml" style="width:150px">
							<label style="width:70px" class="readable">Zip Code:</label>
							<input class="data" type="text" name="zip" style="width:60px" value="<?php echo $zip ?>"/>
						</div>
					</td>
				</tr>
			</table>
		</div>
		<br/>
<?php 
        # allow user to update existing consumer meta-data
        #or cancel and get back to consumer search form.
	 #this assumes no VAIL form is in the current view
   
	if(!$form) {     
  ?>
  	    <p style="text-align:right">  
			<input class="btn" 
						style="background-color: #fff;
								     background-image:url('./images/gt/btn-blank-long.jpg');
									background-repeat:no-repeat;
									border:none;
									width:160px;
									height: 30px;
									color: #fff;
									font-weight:bold;"
					    type="submit" 
						value="<?php echo $btnName; ?>" />
			<input class="btn" 
					 style="background-color: #fff;
								background-image:url('./images/gt/btn-blank-long.jpg');
								background-repeat:no-repeat;
								border:none;
								width:160px;
								height: 30px;
								color: #fff;
								font-weight:bold;"
						type="button" value="Cancel" 
      	                onclick="window.location='admin.php?action=cie';"/>
		</p>
<?php 
	} 
	
		#the value attribute for the hidden field needs to be changed.  $id sucks.  i need to 
		#put are more descriptive variable name.
  ?>
		<input type="hidden" value="<?php echo $id;?>" name="id" />
   </form>
   <!-- END CONSUMER METADATA FORM -->     

 <?php      
	###################
	# FORM CONTROLLER  	#
	############################################################################
	#	this block determines the processing block in current view.   it needs to be updated anytime a new form type
	#	is created.  $form's value is provided as GET in the url.
	#	when the 'include' is triggered, the selected form renders the interface below the consumer tab area.
	#	if no form is in current view, the contoller is presented to the user.
	############################################################################
   
	#[0] Intake Page 1
	if(strcmp($form,"intake1")==0) {  
		include('forms/intake1.php');
	
	#[1] Intake Page 2
	} else if(strcmp($form, "intake2")==0) {
		include('forms/intake2.php');
   
  	#[2] Independent Living Plan
	} else if(strcmp($form, "ilpcsr")==0) {
		include('forms/ilpcsr.php');
   
	#[3] Authorization to Release Information 
	} else if(strcmp($form, "authToRelease")==0) {
		include('forms/authToRelease.php');
   
 	#[4] Case Note List - still using Monthly ISR terminology   : ^(
	} else if(strcmp($form, "monthlyISRList")==0) {
		include('forms/monthlyISRList.php');
 
 	#[5] Case Note Form
	} else if(strcmp($form, "monthlyISR")==0) {
		include('forms/monthlyISR.php');
   
 	#[6] Information and Referral Entry
	} else if(strcmp($form, "irEntry")==0) {
		include('forms/irEntry.php');
   
 	#[7] Program Enrollment 
	} else if(strcmp($form, "enroll")==0) {
		include('forms/enroll.php');
   
	#######################################
	# 	no form in in view, offer the user form choices	.  		#
	#  the form contoller will only show when an consumer     #
	#   record was found in the database.						#
	#######################################
	} else if(!$noRecord) {     
 ?>
      <div id="formUnit" style="border-top:1px #ccc dotted;padding-top:25px;">
		<div style="margin-left:350px;">
			<label style="padding:15px; text-align:center; font-weight:bold;">
				Grab A Case File Form  To Work On:
			</label><br />
			<select name="entryForm" id="entryForm">
				<option value=""></option>
				<option value="intake1"><?php echo THIS_CIL ;?> Intake (Page 1)</option>
				<option value="intake2"><?php echo THIS_CIL ;?> Intake (Page 2)</option>
				<option value="authToRelease">Authorization To Release Information</option>
				<option value="ilpcsr"><?php echo THIS_CIL ;?> ILP/CSR</option>
				<!-- <option value="enroll">Enroll In Programs</option> -->
				<option value="monthlyISR">Monthly Case Notes</option>
				<option value="irEntry">Consumer I&amp;R</option>
			</select>
			<input type="button" 
						style="background-color: #fff;
								background-image:url('./images/gt/btn-blank-long.jpg');
								 background-repeat:no-repeat;
								 border:none;
								 width:160px;
								 height: 30px;
								 color: #fff;
								 font-weight:bold;"
						class="btn" value="Load Form" onclick="chooseForm($('entryForm'));"/>
		</div>
	</div>
	
<?php
	# 1 have mixed feelings here.  js used below is scriptaculous.  however, jquery powndz!!!!
	# i may need to update this to run jquery, or i may eliminate the cotroller interface so
	# it's not soooo ARCIL-like.
?>
      <!-- THIS IS THE DATA ENTRY FORM CONTROLLER FROM SELECT OPTION -->
      <script type="text/javascript">
         function chooseForm(obj) {
            value = obj.options[obj.selectedIndex].value;
            if(value=='intake1') {
               window.location='admin.php?action=cieedit&id=<?php echo $id; ?>&form=intake1';   
            } else if(value=='intake2') {
               window.location='admin.php?action=cieedit&id=<?php echo $id; ?>&form=intake2';
            } else if(value=='ilpcsr') {
               window.location='admin.php?action=cieedit&id=<?php echo $id; ?>&form=ilpcsr';
            } else if(value=='authToRelease') {
	     	    window.location='admin.php?action=cieedit&id=<?php echo $id; ?>&form=authToRelease';
	   	      }  else if(value=='monthlyISR') {
				window.location='admin.php?action=cieedit&id=<?php echo $id; ?>&form=monthlyISRList';
	   	      } else if(value=='irEntry') {
				window.location='admin.php?action=cieedit&id=<?php echo $id; ?>&form=irEntry';
             //}else if(value=='enroll') {
				//window.location='admin.php?action=cieedit&id=<?php echo $id; ?>&form=enroll';
             }
         }
      </script>
<?php 
	} #this ends the form controller code block
?>